package com.pingtai.indent.action;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.pingtai.ajax.entity.IndentTarget_163;
import com.pingtai.ajax.service.NoticeService;
import com.pingtai.filter.ChildUserFilter;
import com.pingtai.indent.entity.BatchIndentAddMsg;
import com.pingtai.indent.entity.BatchIndentItem;
import com.pingtai.indent.entity.IndentQqPlzb;
import com.pingtai.indent.entity.IndentQqPlzbType;
import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndent;
import com.pingtai.indent.entity.UserIndentMoneyTmp;
import com.pingtai.indent.entity.UserIndentSearch;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.indent.service.WeiboIndentService;
import com.pingtai.user.entity.User;
import com.pingtai.user.entity.UserPay;
import com.pingtai.user.service.UserManagerService;
import com.pingtai.util.PaginationSupport;
import com.pingtai.util.Utils;

public class WeiBoIndentAction {

	private WeiboIndentService weiboIndentService;
	private UserManagerService userManagerService;
	private User curUser;
	private PaginationSupport paginationSupport;
	
	private int currentPage=1;
	private int pageNum;
	private String queryString;
	private UserIndentSearch qe;
	private String current;
	
	private UserBaseIndentType baseIndentType;
	
	private Float price;
	private UserIndent userIndent;
	private UserIndentType userIndentType;
	
	private String batchBills;
	
	private NoticeService noticeService;
	
	private BatchIndentAddMsg batchIndentAddMsg = new BatchIndentAddMsg();
	
	private String qq;
	
	private List indentNumberTmpList;
	
	private List indentQqPlzbTypeList;
	
	private IndentQqPlzbType indentQqPlzbType;
	private IndentTarget_163 indentTarget;
	private String descp;
	
	private Float totalPrice;
	private List baseIndentTypeList;
	private List indentTypeList;
	private List priceList;
	private Integer size;
	private List baseIndentTypeMinAmountList;
	private List baseIndentTypeMaxAmountList;
	private List baseIndentTypeUnitPriceList;
	private List baseIndentTypeNameList;
	private List baseIndentTypeUnitNameList;
private String typeRadio;
	
	public String getTypeRadio() {
		return typeRadio;
	}
	public void setTypeRadio(String typeRadio) {
		this.typeRadio = typeRadio;
	}
	
	public List getBaseIndentTypeMinAmountList() {
		return baseIndentTypeMinAmountList;
	}

	public void setBaseIndentTypeMinAmountList(List baseIndentTypeMinAmountList) {
		this.baseIndentTypeMinAmountList = baseIndentTypeMinAmountList;
	}

	public List getBaseIndentTypeMaxAmountList() {
		return baseIndentTypeMaxAmountList;
	}

	public void setBaseIndentTypeMaxAmountList(List baseIndentTypeMaxAmountList) {
		this.baseIndentTypeMaxAmountList = baseIndentTypeMaxAmountList;
	}

	public List getBaseIndentTypeUnitPriceList() {
		return baseIndentTypeUnitPriceList;
	}

	public void setBaseIndentTypeUnitPriceList(List baseIndentTypeUnitPriceList) {
		this.baseIndentTypeUnitPriceList = baseIndentTypeUnitPriceList;
	}

	public List getBaseIndentTypeNameList() {
		return baseIndentTypeNameList;
	}

	public void setBaseIndentTypeNameList(List baseIndentTypeNameList) {
		this.baseIndentTypeNameList = baseIndentTypeNameList;
	}

	public List getBaseIndentTypeUnitNameList() {
		return baseIndentTypeUnitNameList;
	}

	public void setBaseIndentTypeUnitNameList(List baseIndentTypeUnitNameList) {
		this.baseIndentTypeUnitNameList = baseIndentTypeUnitNameList;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public List getBaseIndentTypeList() {
		return baseIndentTypeList;
	}

	public void setBaseIndentTypeList(List baseIndentTypeList) {
		this.baseIndentTypeList = baseIndentTypeList;
	}

	public List getIndentTypeList() {
		return indentTypeList;
	}

	public void setIndentTypeList(List indentTypeList) {
		this.indentTypeList = indentTypeList;
	}

	public List getPriceList() {
		return priceList;
	}

	public void setPriceList(List priceList) {
		this.priceList = priceList;
	}

	public Float getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(Float totalPrice) {
		this.totalPrice = totalPrice;
	}


	

	public String getDescp() {
		return descp;
	}

	public void setDescp(String descp) {
		this.descp = descp;
	}

	public IndentTarget_163 getIndentTarget() {
		return indentTarget;
	}

	public void setIndentTarget(IndentTarget_163 indentTarget) {
		this.indentTarget = indentTarget;
	}

	public IndentQqPlzbType getIndentQqPlzbType() {
		return indentQqPlzbType;
	}

	public void setIndentQqPlzbType(IndentQqPlzbType indentQqPlzbType) {
		this.indentQqPlzbType = indentQqPlzbType;
	}

	public List getIndentQqPlzbTypeList() {
		return indentQqPlzbTypeList;
	}

	public void setIndentQqPlzbTypeList(List indentQqPlzbTypeList) {
		this.indentQqPlzbTypeList = indentQqPlzbTypeList;
	}

	public List getIndentNumberTmpList() {
		return indentNumberTmpList;
	}

	public void setIndentNumberTmpList(List indentNumberTmpList) {
		this.indentNumberTmpList = indentNumberTmpList;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public BatchIndentAddMsg getBatchIndentAddMsg() {
		return batchIndentAddMsg;
	}

	public void setBatchIndentAddMsg(BatchIndentAddMsg batchIndentAddMsg) {
		this.batchIndentAddMsg = batchIndentAddMsg;
	}

	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public String getBatchBills() {
		return batchBills;
	}

	public void setBatchBills(String batchBills) {
		this.batchBills = batchBills;
	}

	public UserIndentType getUserIndentType() {
		return userIndentType;
	}

	public void setUserIndentType(UserIndentType userIndentType) {
		this.userIndentType = userIndentType;
	}

	public UserIndent getUserIndent() {
		return userIndent;
	}

	public void setUserIndent(UserIndent userIndent) {
		this.userIndent = userIndent;
	}

	public Float getPrice() {
		return price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

	public UserBaseIndentType getBaseIndentType() {
		return baseIndentType;
	}

	public void setBaseIndentType(UserBaseIndentType baseIndentType) {
		this.baseIndentType = baseIndentType;
	}

	public String getCurrent() {
		return current;
	}

	public void setCurrent(String current) {
		this.current = current;
	}

	public UserIndentSearch getQe() {
		return qe;
	}

	public void setQe(UserIndentSearch qe) {
		this.qe = qe;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public PaginationSupport getPaginationSupport() {
		return paginationSupport;
	}

	public void setPaginationSupport(PaginationSupport paginationSupport) {
		this.paginationSupport = paginationSupport;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	private String base;
	
	

	public String getBase() {
		return base;
	}

	public void setBase(String base) {
		this.base = base;
	}

	public WeiboIndentService getWeiboIndentService() {
		return weiboIndentService;
	}

	public void setWeiboIndentService(WeiboIndentService weiboIndentService) {
		this.weiboIndentService = weiboIndentService;
	}
	
	public String weiboManageUIFor163(){
		
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		if(userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,new Integer(base));
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,new Integer(base));
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		
	    
		return "success";
	}
	
	public String weiboManageUIForSearch_163(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			try {
				qe = weiboIndentService.paseQueryString(qe,queryString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,new Integer(base),qe);
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,new Integer(base),qe);
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		return "success";
	}
	
	public String toWeiboAddFor163(){
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		if(baseIndentType.getEnable()==0){
			return "produceWeihu";
		}
		curUser = userManagerService.findCurrectUser();
		if(userManagerService.isRoot(curUser)){
			price = baseIndentType.getPrice();
		}else{
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			if(indentType==null){
				return "403";
			}else{
				indentType = weiboIndentService.updateIndentType(indentType);
				price = indentType.getPrice();
			}
		}
		return "success";
	}
	
	public String addIndent_163(){
			//判断价钱是否合法
			User curUser = userManagerService.findCurrectUser();
			boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
			if(flag){
				
				userIndent.setCreateDate(new Date());
				userIndent.setNowNumber(userIndent.getStartNumber());
				User parentUser = userManagerService.findUserById(curUser.getParentUserId());
				userIndent.setParentUser(parentUser);
				userIndent.setState(0);
				
				baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
				if(userManagerService.isRoot(curUser)){
					price = baseIndentType.getPrice();
				}else{
					UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
					if(indentType==null){
						return "403";
					}else{
						Float oldPrice = indentType.getPrice();
						indentType = weiboIndentService.updateIndentType(indentType);
						price = indentType.getPrice();
						if(!oldPrice.equals(price)){
							return "priceChange";
						}
					}
				}
				
				userIndent.setType(new Integer(base));
				userIndent.setUser(curUser);
				Float t = price*userIndent.getNumber()/baseIndentType.getUnit2();
				userIndent.setTotalPrice(t);
				userIndent.setPrice(price);
				//判余额是否充足
				Float money = curUser.getMoney();
				Float money2 = (float) Math.round(money*100/100);
				if(money2<t){
					return "moneyLow";
				}
				userIndent.setBasePrice(baseIndentType.getPrice());
				Float baseTotalPrice = baseIndentType.getPrice()*userIndent.getNumber()/baseIndentType.getUnit2();
				userIndent.setBaseTotalPrice(baseTotalPrice);
				userIndent.setBaseIndentType(baseIndentType);
				//保存订单表
				userIndent = weiboIndentService.saveUserIndent(userIndent);
				//所需金额保存到临时表
				UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
				userIndentMoneyTmp.setMoney(t);
				userIndentMoneyTmp.setUser(curUser);
				userIndentMoneyTmp.setUserIndent(userIndent);
				userIndentMoneyTmp = weiboIndentService.saveUserIndentMoneyTmp(userIndentMoneyTmp);
				//从用户账户中的总金额减去订单总价 保存
				money = curUser.getMoney();
				money = money -t;
				curUser.setMoney(money);
				UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
				
				weiboIndentService.getIndentDao().evict(userIndent);
				weiboIndentService.getIndentDao().evict(userIndentMoneyTmp);
				weiboIndentService.getIndentDao().evict(userPay);
				
				
				//保存返利关系
				UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
				weiboIndentService.saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
				
				//批量添加反射使用
				totalPrice = userIndent.getTotalPrice();
				userIndent=null;
				
			}else{
				return "add_indent_403";
			}
		
		return "success";
	}
	
	public String toIndentRefund(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	
	public String indentRefund(){
		UserIndent userIndent1 = weiboIndentService.getUserIndentById(userIndent.getId());
		userIndent1.setRefundReason(userIndent.getRefundReason());
		User curUser = userManagerService.findCurrectUser();
		userIndent1.setState(userIndent.getState());
		userIndent1.setRefundDate(new Date());
		userIndent1.setRefundUser(curUser);
		weiboIndentService.saveUserIndent(userIndent1);
		UserPay userPay = new UserPay();
		
		User childUser = userIndent1.getUser();
		userPay.setChildUser(childUser);
		userPay.setUser(curUser);
		userPay.setCreateDate(new Date());
		userPay.setMoney(userIndent1.getTotalPrice());
		userPay.setType(1);
		userPay.setUserAllMoney(childUser.getMoney()+userIndent1.getTotalPrice());
		userPay.setUserIndent(userIndent1);
		userPay.setType2(4);
		userPay.setUserBaseIndent(userIndent1.getBaseIndentType());
		userPay.setIndentCreateUser(userIndent1.getUser());
		userManagerService.saveUserPay(userPay);
		childUser.setMoney(childUser.getMoney()+userIndent1.getTotalPrice());
		userManagerService.saveOrUpdateUser(childUser);
		baseIndentType = userIndent1.getBaseIndentType();
		return "success";
	}
	
	public String toIndentDetails(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		indentNumberTmpList = weiboIndentService.getIndentNumberTmp(userIndent);
		return "success";
	}
	
	public String toBatchIndentAdd(){
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		int id = baseIndentType.getId();
		baseIndentType = weiboIndentService.getBaseIndentTypeById(id);
		
		if(baseIndentType.getEnable()==0){
			return "produceWeihu";
		}
		
		User curUser = userManagerService.findCurrectUser();
		if(userManagerService.isRoot(curUser)){
			price = baseIndentType.getPrice();
		}else{
			price = weiboIndentService.getUserIndentTypeByUserAndType(curUser, id).getPrice();
		}
		return "success";
	}
	
	public String batchIndentAdd() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		String[] arrays=null;
		if(batchBills!=null&&batchBills.length()>0){
			arrays = batchBills.split("\r\n");
		}
		if(arrays==null) return "500";
		baseIndentType = weiboIndentService.getBaseIndentTypeById(baseIndentType.getId());
		String pre = baseIndentType.getPre();
		Class clazz = this.getClass();
		Class clazz2 = noticeService.getClass();
		String me = "addIndent_"+ baseIndentType.getBatchMehod();
		String me2 = "getIndentTarget_"+baseIndentType.getBatchMehod();
		Method method = clazz.getDeclaredMethod(me);
		Method method2 = clazz2.getDeclaredMethod(me2,String.class);
		for(int i=0;i<arrays.length;i++){
			String url = null;
			BatchIndentItem batchIndentItem = null;
			try {
				String[] array = arrays[i].split("\\+");
				 url = pre + array[0];
				String amount = array[1];
				if(new Integer(amount)<1000){
					amount = "1000";
				}
				userIndent= new UserIndent();
				userIndent.setNumber(new Integer(amount));
				userIndent.setBaseIndentType(baseIndentType);
				userIndent.setTargetUrl(url);
			} catch (Exception e) {
				batchIndentItem = new BatchIndentItem();
				batchIndentItem.setUrlAndNumber(url);
				batchIndentItem.setType(2);
				batchIndentAddMsg.getItems().add(batchIndentItem);
				batchIndentAddMsg.setFail(batchIndentAddMsg.getFail()+1);
				continue;
			}
			IndentTarget_163 indentTarget_163 = null;
			try {
				 indentTarget_163 = (IndentTarget_163) method2.invoke(noticeService, url);
			} catch (Exception e) {
				batchIndentItem = new BatchIndentItem();
				batchIndentItem.setUrlAndNumber(arrays[i]);
				batchIndentItem.setType(1);
				batchIndentAddMsg.getItems().add(batchIndentItem);
				batchIndentAddMsg.setFail(batchIndentAddMsg.getFail()+1);
				continue;
			}
			
			userIndent.setTargetTitle(indentTarget_163.getName());
			userIndent.setStartNumber(new Integer(indentTarget_163.getNumber()));
			//try {
				method.invoke(this);
			//} catch (Exception e) {
			//	e.printStackTrace();
			//	batchIndentItem = new BatchIndentItem();
			//	batchIndentItem.setUrlAndNumber(arrays[i]);
			//	batchIndentItem.setType(2);
			//	batchIndentAddMsg.getItems().add(batchIndentItem);
			//	batchIndentAddMsg.setFail(batchIndentAddMsg.getFail()+1);
			//	continue;
			//}
			
			
			batchIndentItem = new BatchIndentItem();
			batchIndentItem.setUrlAndNumber(url);
			batchIndentItem.setType(0);
			batchIndentAddMsg.getItems().add(batchIndentItem);
			batchIndentAddMsg.setSuccess(batchIndentAddMsg.getSuccess()+1);
			batchIndentAddMsg.setTotalMoney(batchIndentAddMsg.getTotalMoney()+totalPrice);
		}
		return "success";
	}
	
	public String toIndentComplete(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	public String toWeixinIndentComplete(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}

	public String indentComplete(){
		int nowNumber = userIndent.getNowNumber();
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		baseIndentType = userIndent.getBaseIndentType();
		User completeUser = userManagerService.findCurrectUser();
		//调用返利方法
		try {
			userManagerService.userIndentRebate(userIndent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		userIndent.setState(2);
		userIndent.setNowNumber(nowNumber);
		userIndent.setEndDate(new Date());
		userIndent.setEndUser(completeUser);
		weiboIndentService.updateUserIndentForCompleate(userIndent);
		return "success";
	}
	
	public String WeixinIndentComplete(){
		int nowNumber = userIndent.getNowNumber();
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		baseIndentType = userIndent.getBaseIndentType();
		User completeUser = userManagerService.findCurrectUser();
		//调用返利方法
		try {
			userManagerService.userIndentRebate(userIndent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		userIndent.setState(2);
		userIndent.setNowNumber(nowNumber);
		userIndent.setEndDate(new Date());
		userIndent.setEndUser(completeUser);
		weiboIndentService.updateUserIndentForCompleate(userIndent);
		return "success";
	}
	
	public String weiboManageUIForQq(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		List baseList = new ArrayList();
		Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
		Iterator childIt = childBaseIndentType.iterator();
		while(childIt.hasNext()){
			UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
			baseList.add(ubt.getId());
		}
		
		if(userManagerService.isNotUser(curUser)&&!userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,baseList);
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,baseList);
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		
	    
		return "success";
	}
	
	public String weiboManageUIForSearch_qq(){
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			try {
				qe = weiboIndentService.paseQueryString(qe,queryString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		List baseList = new ArrayList();
		Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
		Iterator childIt = childBaseIndentType.iterator();
		while(childIt.hasNext()){
			UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
			baseList.add(ubt.getId());
		}
		
		
		if(userManagerService.isNotUser(curUser)&&!userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,baseList,qe);
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,baseList,qe);
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		return "success";
	}
	
	public String toWeiboAddForQq(){
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		baseIndentTypeList = new ArrayList();
		priceList = new ArrayList();
		
		baseIndentTypeMinAmountList= new ArrayList();
		baseIndentTypeMaxAmountList= new ArrayList();
		baseIndentTypeUnitPriceList= new ArrayList();
		baseIndentTypeNameList= new ArrayList();
		baseIndentTypeUnitNameList= new ArrayList();
		
		
		indentTypeList = new ArrayList();
		if(baseIndentType.getEnable()==0){
			return "produceWeihu";
		}
		
		
		curUser = userManagerService.findCurrectUser();
		if(userManagerService.isRoot(curUser)){
			
			Set s = baseIndentType.getChildrenBaseIndentType();
			Iterator it = s.iterator();
			while(it.hasNext()){
				UserBaseIndentType b = (UserBaseIndentType) it.next();
				if(b.getEnable()!=1) continue;
				baseIndentTypeList.add(b);
			}
			
			if(baseIndentTypeList!=null&&baseIndentTypeList.size()>0){
				for(int i=0;i<baseIndentTypeList.size();i++){
					UserBaseIndentType b = (UserBaseIndentType) baseIndentTypeList.get(i);
					Float p = b.getPrice();
					priceList.add(p);
				}
			}else{
				price = baseIndentType.getPrice();
			}
			
		}else{
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			if(indentType==null){
				return "403";
			}else{
				
				Set s2 = indentType.getChildrenIndentType();
				Iterator it2 = s2.iterator();
				while(it2.hasNext()){
					UserIndentType idt = (UserIndentType) it2.next();
					if(idt.getUserBaseIndentType().getEnable()!=1) continue;
					indentTypeList.add(idt);
				}
				
				
				if(indentTypeList!=null&&indentTypeList.size()>0){
					indentTypeList = Utils.menuSort(indentTypeList);
					for(int i=0;i<indentTypeList.size();i++){
						UserIndentType idt = (UserIndentType) indentTypeList.get(i);
						UserBaseIndentType bdt = idt.getUserBaseIndentType();
						baseIndentTypeList.add(bdt);
						idt = weiboIndentService.updateIndentType(idt);
						Float p = idt.getPrice();
						priceList.add(p);
						baseIndentTypeMinAmountList.add(bdt.getMinAmount());
						baseIndentTypeMaxAmountList.add(bdt.getMaxAmount());
						baseIndentTypeUnitPriceList.add(bdt.getUnit2());
						baseIndentTypeNameList.add(bdt.getName());
						baseIndentTypeUnitNameList.add(bdt.getUnit());
					}
				}else{
					price = indentType.getPrice();
				}
				
				
			}
		}
		return "success";
	}
	
	public String addIndent_qq(){
		//判断价钱是否合法
		User curUser = userManagerService.findCurrectUser();
		boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
		if(flag){
			
			baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
			String s = weiboIndentService.saveIndentForQq(userIndent,curUser,base,qq,baseIndentType);
			return s;
		}else{
			return "add_indent_403";
		}
	}
	public String toIndentDetails_qq(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		indentNumberTmpList = weiboIndentService.getIndentNumberTmp(userIndent);
		qq = weiboIndentService.getQqNumber(userIndent.getId());
		return "success";
	}
	
	public String weiboManageUIForQqzbpl() {
		baseIndentType = weiboIndentService.getBaseIndentTypeById(new Integer(
				base));
		
		curUser = userManagerService.findCurrectUser();
		if (currentPage > 0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE * (currentPage - 1);
		}
		if (userManagerService.isNotUser(curUser)&&!userManagerService.isUser_2(curUser)) {
			paginationSupport = weiboIndentService.getIndentForPage(
					currentPage, true, new Integer(base));
		} else {
			paginationSupport = weiboIndentService.getIndentForPage(
					currentPage, false, new Integer(base));
		}
		if (qe != null) {
			current = qe.getStatus();
		} else {
			current = "all";
		}

		return "success";
	}
	
    public String weiboManageUIForSearch_qq_zbpl(){
    	baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		curUser = userManagerService.findCurrectUser();
		if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		
		if(queryString!=null&&!queryString.isEmpty()){
			try {
				qe = weiboIndentService.paseQueryString(qe,queryString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)){
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,true,new Integer(base),qe);
		}else{
			paginationSupport = weiboIndentService.getIndentForPage(currentPage,false,new Integer(base),qe);
		}
		if(qe!=null){
			current = qe.getStatus();
		}else{
			current = "all";
		}
		return "success";
    }
    
    public String toWeiboAddForQqZbpl(){
    	if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
    	baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
    	
    	if(baseIndentType.getEnable()==0){
			return "produceWeihu";
		}
    	
		curUser = userManagerService.findCurrectUser();
		if(userManagerService.isRoot(curUser)){
			price = baseIndentType.getPrice();
		}else{
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			if(indentType==null){
				return "403";
			}else{
				indentType = weiboIndentService.updateIndentType(indentType);
				price = indentType.getPrice();
			}
		}
		indentQqPlzbTypeList = weiboIndentService.getQqPlzbTypeList();
		return "success";
    }
    
    public String addIndent_qq_zbpl(){
    	//判断价钱是否合法
    			User curUser = userManagerService.findCurrectUser();
    			boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
    			if(flag){
    				
    				userIndent.setCreateDate(new Date());
    				indentQqPlzbType = weiboIndentService.getQqPlzbTypeById(indentQqPlzbType.getId());
    				if(indentQqPlzbType.getName().contains("转播")){
    					userIndent.setStartNumber(new Integer(indentTarget.getZbNumber()));
    				}else if(indentQqPlzbType.getName().contains("评论")){
    					userIndent.setStartNumber(new Integer(indentTarget.getPlNumber()));
    				}
    				userIndent.setNowNumber(userIndent.getStartNumber());
    				User parentUser = userManagerService.findUserById(curUser.getParentUserId());
    				userIndent.setParentUser(parentUser);
    				userIndent.setState(0);
    				
    				baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
    				if(userManagerService.isRoot(curUser)){
    					price = baseIndentType.getPrice();
    				}else{
    					UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
    					if(indentType==null){
    						return "403";
    					}else{
    						Float oldPrice = indentType.getPrice();
    						indentType = weiboIndentService.updateIndentType(indentType);
    						price = indentType.getPrice();
    						if(!oldPrice.equals(price)){
    							return "priceChange";
    						}
    					}
    				}
    				
    				IndentQqPlzb indentQqPlzb = new IndentQqPlzb();
    				descp = Utils.replaceEnter(descp);
    				indentQqPlzb.setDescp(descp);
    				indentQqPlzb.setIndentQqPlzbType(indentQqPlzbType);
    				indentQqPlzb = weiboIndentService.saveIndentQqPlzb(indentQqPlzb);
    				
    				userIndent.setIndentQqPlzb(indentQqPlzb);
    				userIndent.setType(new Integer(base));
    				userIndent.setUser(curUser);
    				Float t = price*userIndent.getNumber()/baseIndentType.getUnit2();;
    				userIndent.setTotalPrice(t);
    				userIndent.setPrice(price);
    				//判余额是否充足
    				Float money = curUser.getMoney();
    				Float money2 = (float) Math.round(money*100/100);
    				if(money2<t){
    					return "moneyLow";
    				}
    				userIndent.setBasePrice(baseIndentType.getPrice());
					Float baseTotalPrice = baseIndentType.getPrice()*userIndent.getNumber()/baseIndentType.getUnit2();
					userIndent.setBaseTotalPrice(baseTotalPrice);
    				userIndent.setBaseIndentType(baseIndentType);
    				//保存订单表
    				userIndent = weiboIndentService.saveUserIndent(userIndent);
    				
    				//保存腾讯微博订单对应QQ号码
    				
    				//weiboIndentService.saveIndentQq(qq,userIndent);
    				
    				//所需金额保存到临时表
    				UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
    				userIndentMoneyTmp.setMoney(t);
    				userIndentMoneyTmp.setUser(curUser);
    				userIndentMoneyTmp.setUserIndent(userIndent);
    				userIndentMoneyTmp = weiboIndentService.saveUserIndentMoneyTmp(userIndentMoneyTmp);
    				//从用户账户中的总金额减去订单总价 保存
    				money = curUser.getMoney();
    				money = money -t;
    				curUser.setMoney(money);
    				UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
    				
    				weiboIndentService.getIndentDao().evict(userIndent);
    				weiboIndentService.getIndentDao().evict(userIndentMoneyTmp);
    				weiboIndentService.getIndentDao().evict(userPay);
    				
    				
    				//保存返利关系
    				UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
    				weiboIndentService.saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
    				//userIndent2 = userIndent;
    				userIndent=null;
    				
    			}else{
    				return "add_indent_403";
    			}
    		
    		return "success";
    }
    
    public String toIndentDetails_qq_zbpl(){
    	userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		indentNumberTmpList = weiboIndentService.getIndentNumberTmp(userIndent);
		return "success";
    }
    
    public String produceDeveloping(){
    	return "success";
    }
    
    public String showUserConsumption(){
    	curUser = userManagerService.findUserById(curUser.getUserId());
    	if(currentPage>0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE*(currentPage-1);
		}
		paginationSupport = weiboIndentService.getUserConsumptionForPage(currentPage,curUser);
		current = "all";
		
		return "success";
    }
}

